 
 import * as config from './index.js'
 import Vue from 'vue'
 
 let loginLock = false
 let userInfo = uni.getStorageSync('userInfo');
 export const msg = (title, duration = 1500, mask = false, icon = 'none') => {
 	//统一提示方便全局修改
 	if (Boolean(title) === false) {
 		return;
 	}
 	uni.showToast({
 		title,
 		duration,
 		mask,
 		icon
 	});
 }
 
 export const request = (method, endpoint, data = {}, failCallback,header = {},) => {
	//异步请求数据
	return new Promise(resolve => {
			userInfo = uni.getStorageSync('userInfo')
		let accessToken = userInfo ? userInfo.accessToken : ''
		let addresses = uni.getStorageSync('addresses') || '';
		let baseUrl = config.def().baseUrl
		// console.log(baseUrl,'baseUrl')
        let content = 'application/x-www-form-urlencoded; charset=UTF-8';
        if (method === 'post') {
            content = 'application/json; charset=UTF-8';
        }
		if (method === 'put') {
		    content = 'application/json; charset=UTF-8';
		}
		// if (method === 'delete') {
		//     content = 'application/json; charset=UTF-8';
		// }
		let head = Object.assign({
				'Content-Type': content,
				'Authorization': 'Bearer '+ accessToken
			},header)
		
		
		uni.request({
			url: baseUrl+"/"+ endpoint,
			method: method,
			data: data,
			header: head,
			success: (res) => {
                if (res.data.code === 200) {
					//#ifdef H5
					// if((!addresses || !addresses.addressesName) && accessToken){
					// 	uni.showModal({
					// 		title: '地图提示',
					// 		content: '您尚未选择，您的位置？',
					// 		showCancel: true,
					// 		confirmText: '选择',
					// 		success: (e) => {
					// 			if (e.confirm) {
					// 				uni.chooseLocation({
					// 					success: (res1) => {
					// 						console.log(res1)
					// 						if (res1.name === "") {
					// 							return
					// 						}
					// 						let addressesInfo = {};
					// 						addressesInfo.lng = res1.longitude;
					// 						addressesInfo.lat = res1.latitude;
					// 						addressesInfo.addressesName = res1.name;
					// 						uni.setStorageSync('addresses', addressesInfo);
					// 						uni.reLaunch({
					// 							url: '/pages/index/index?type=1'
					// 						});
					// 					},fail: (e) => {
					// 						console.log(e)
					// 					}
					// 				});
					// 			}
					// 		},
					// 		fail: () => {}
					// 	})
					// }
					//#endif
                    resolve(res.data);
                } else if (res.data.code === 401) {
                    if (failCallback) {
                        failCallback(res.data)
                    }
                    if (!loginLock) {
                        loginLock = true
                        uni.showModal({
                            title: '登录提示',
                            content: '您尚未登录，是否立即登录？',
                            showCancel: true,
                            confirmText: '登录',
                            success: (e) => {
                                if (e.confirm) {
                                    uni.navigateTo({
                                        url: '/pages/public/login'
                                    })
                                }
                            },
                            fail: () => {},
                            complete: () => {
                                loginLock = false
                            }
                        })
                    }

                } else {
                    if (failCallback) {
                        failCallback(res.data)
                    } else {
                        uni.showToast({
                            title: res.data.msg,
                            icon: 'none'
                        })
                    }
                }
			}
		})
	})
}



export const uploadImg = (num = 9,successCallback) => {
    userInfo = uni.getStorageSync('userInfo');
	var accessToken = userInfo ? userInfo.accessToken : '';
	let baseUrl = config.def().baseUrl
	uni.chooseImage({
		count: 1,
		sourceType: ['album','camera'],
		success: res => {
			uni.getImageInfo({
				src: res.tempFilePaths[0],
				success: image => {
					console.log(image)
					uni.showLoading({ title: '图片上传中', mask: true })
					uni.uploadFile({
						url: baseUrl + `/oss/app/upload`,
						file: image,
						filePath: image.path,
						header: {
							Authorization: 'Bearer ' + accessToken,
						},
						name: 'file',
						success: res => {
							if (successCallback) {
								successCallback(JSON.parse(res.data).data.url)
							}
						},
						fail: err => {
							uni.showToast({
								title: '上传图片失败',
								icon: 'none',
								duration: 2000,
							})
						},
						complete: res => {
							uni.hideLoading()
						},
					})
				},
				fail: err => {
					uni.showToast({
						title: '获取图片信息失败',
						icon: 'none',
						duration: 2000,
					})
				},
			})
		},
	})
}


export const prePage = () => {
	console.log('prePage')
	let pages = getCurrentPages();
	let prePage = pages[pages.length - 2];
	// #ifdef H5
	return prePage;
	// #endif
	return prePage.$vm;
}


export const isVip = () => {
	return userInfo && userInfo.level
}

export const setUserInfo = (i) => {
	userInfo = i
}

export const logout = () => {
	userInfo = undefined
	uni.removeStorage({
		key: 'userInfo'
	})
}